import Toast from "../../component/vant/toast/toast";

var app = getApp();
const util = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    last_id: 0,
    downloadUrl: app.api.xh.downloadDomain,
    question_html: app.api.xh.domain,
    questions: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var page = this;
    var title = '错题集';
    page.setData({
      id: options.id
    })
    if (options.name != undefined) {
      title += '-' + options.name;
    }
    app.setPageTitle(title);
    page.getErrQuestion()
  },

  //  错题
  getErrQuestion: function () {
    var page = this;
    var param = {
      error_book_id: page.data.id
    }
    if (page.data.last_id > 0) {
      param.last_id = page.data.last_id
    } else {
      if (page.data.last_id == -1) {
        Toast('没有更多了');
        return false;
      }
    }
    app.sendRequest({
      url: app.api.student.errbook_info,
      data: param,
      success(res) {
        var questions = page.data.questions;
        var questions_load = res.data.list;
        questions_load == undefined ? [] : questions_load;
        var last_id = 0;
        if (page.data.total == undefined && res.data.pagenation != undefined) {
          page.setData({
            total: res.data.pagenation.total
          })
        }
        if (questions_load !== undefined) {
          last_id = res.data.last_id;
          questions = questions.concat(questions_load);
          page.setData({
            questions: questions,
            last_id: last_id
          });
          page.processHtml();
        }
      }
    });
  },
  toggleCatalog: function (e) {
    var page = this;
    var expand_status = e.currentTarget.dataset.expand;
    var id = e.currentTarget.dataset.id,
        questions = page.data.questions;

    for (var i = 0; i < questions.length; i++) {
      if (questions[i].uuid == id) {
        questions[i].expand = !expand_status;
      }
    }
    page.setData({
      questions: questions
    });
  },
  //html转wxml处理
  processHtml: function () {
    var page = this;
    var questions = page.data.questions;
    var stemArr = [];
    var analysisArr = [];

    //获取题干解析html总请求数
    var body_url_cnt = 0,
        body_url_load_cnt = 0;
    var analysis_url_cnt = 0,
        analysis_url_load_cnt = 0;
    questions.map(q => {
      if (q.stem_url != undefined && q.stem_url.length > 0) {
        body_url_cnt++;
      }
      if (q.analysis_url != undefined && q.analysis_url.length > 0) {
        analysis_url_cnt++;
      }
    });

    questions.map((q, index) => {
      if (q.stem_url != undefined && q.stem_url.length > 0) {
        util.get(page.data.downloadUrl + "/" + q.stem_url).then(function (res) {
          body_url_load_cnt++;
          questions[index].stem = res.data.replace(
              /\<img/gi,
              '<img style="max-width:100%;height:auto"'
          );
          if (body_url_load_cnt == body_url_cnt) {
            page.setData({
              questions: questions
            });
          }
        });
      } else {
        questions[index].stem = q.stem.replace(/\<img/gi, '<img style="max-width:100%;height:auto"');
      }
      if (q.analysis_url != undefined && q.analysis_url.length > 0) {
        util
            .get(page.data.downloadUrl + "/" + q.analysis_url)
            .then(function (res) {
              analysis_url_load_cnt++;
              questions[index].analysis = res.data.replace(
                  /\<img/gi,
                  '<img style="max-width:100%;height:auto"'
              );
              if (analysis_url_load_cnt == analysis_url_cnt) {
                page.setData({
                  questions: questions
                });
              }
            });
      } else {
        questions[index].analysis = q.analysis.replace(/\<img/gi, '<img style="max-width:100%;height:auto"');
      }

      // questions[index].seq = index;
      // //题干html转wxml
      // if (q.stem.length > 0) {
      //   stemArr[index] = q.stem;
      // } else {
      //   stemArr[index] = "";
      // }
      // WxParse.wxParse("stem" + index, "html", stemArr[index], page);
      // if (index === questions.length - 1) {
      //   WxParse.wxParseTemArray("stemTemArray", "stem", stemArr.length, page);
      // }
      // //解析html转wxml
      // if (q.analysis.length > 0) {
      //   analysisArr[index] = q.analysis;
      // } else {
      //   analysisArr[index] = "";
      // }
      // WxParse.wxParse("analysis" + index, "html", analysisArr[index], page);
      // if (index === questions.length - 1) {
      //   WxParse.wxParseTemArray(
      //     "analysisTemArray",
      //     "analysis",
      //     analysisArr.length,
      //     page
      //   );
      // }
    });

    page.setData({
      questions: questions
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getErrQuestion()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})